const app = getApp()
import fetch from '../../utils/services'
const Utils = require("../../utils/util.js")
 
Page({
  data: {
    type: '1',
    info: {
    },
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '正在加载',
    })
    app.globalData.page = 'home'
    if (options.q) {
      // 解析网页的链接
      let qrUrl = decodeURIComponent(options.q)
      console.log(qrUrl)
      this.getInfo(Utils.getQueryString(qrUrl,'userCode'))
    } else {
      this.getInfo(options.userCode)
    }
  },
  getInfo: function(options) {
    // const userCode = '32003708f1574d43a870c1fac6eb067d'
    const userCode = options
    fetch.post('/user/detailsByCode', { userCode } ).then(data => {
      wx.hideLoading()
      const info = {
        avator: data.userPic,
        name: data.userName,
        dec: data.signature || '',
        userId: data.userCode
      }
      const userInfo = {
        userId: data.userCode,
        avator: data.userPic,
        // 0：online 在线、1：offline 离线、2：logout 登出 3：忙碌中
        status: data.onlineStatus,
        name: data.userName
      }
      this.setData({ info })
      app.globalData.userInfo = userInfo
    }).catch(err => {
      console.log(err)
      wx.hideLoading()
      wx.showToast({
        title: '获取信息失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  jion: function(e) {
    // wx.navigateTo({
    //   url: `/pages/groupChat/index`,
    //   fail: err => {
    //     console.log(err)
    //   }
    // })
    // return
    if (!this.data.info.userId) {
      wx.showToast({
        title: '获取信息失败, 无法进入会议',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
      title: '正在加载',
    })
    fetch.post('/userContacts/apply', { userCode: this.data.info.userId } ).then(data => {
      console.log(data)
      wx.hideLoading()
      wx.navigateTo({
        url: '/pages/chat/index?from=home&contactsCode='+data.contactsCode,
        fail: err => {
          console.log(err)
        }
      })
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err.message || '获取信息失败',
        icon: 'none',
        duration: 2000
      })
    })
  }
})
